package com.example.coursedesign.ui.dashboard.adapter;

import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.coursedesign.R;
import com.example.coursedesign.ui.dashboard.entity.Science;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ScienceAdapter extends RecyclerView.Adapter<ScienceAdapter.ScienceViewHolder> {

    private final Context context;
    private final List<Science> scienceList = new ArrayList<>();
    private final OnItemClickListener listener;

    public interface OnItemClickListener {
        void onItemClick(Science science);
    }

    public ScienceAdapter(Context context, OnItemClickListener listener) {
        this.context = context;
        this.listener = listener;
    }

    public void setScienceList(List<Science> scienceList) {
        this.scienceList.clear();
        if (scienceList != null) {
            this.scienceList.addAll(scienceList);
        }
        notifyDataSetChanged(); // 更新适配器的数据集
    }

    @NonNull
    @Override
    public ScienceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_science, parent, false);
        return new ScienceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ScienceViewHolder holder, int position) {
        Science science = scienceList.get(position);
        holder.bind(science, listener);
    }

    @Override
    public int getItemCount() {
        return scienceList.size();
    }

    static class ScienceViewHolder extends RecyclerView.ViewHolder {
        private final TextView titleTextView;
        private final TextView publishTimeTextView;
        private final TextView readCountTextView;
        private final ImageView imageView;

        public ScienceViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.title);
            publishTimeTextView = itemView.findViewById(R.id.publishTime);
            readCountTextView = itemView.findViewById(R.id.readCount);
            imageView = itemView.findViewById(R.id.imageView);
        }

        public void bind(final Science science, final OnItemClickListener listener) {
            titleTextView.setText(science.getTitle());

            // 使用 SimpleDateFormat 格式化时间
//            String formattedDate = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(science.getPublish_time());
            publishTimeTextView.setText("发布时间：" + science.getPublish_time());

            readCountTextView.setText("阅读量：" + science.getRead_count());

            // 使用Glide加载图片，假设image字段包含Base64编码的字符串或网络图片URL
            // 如果是Base64，请先解码为Bitmap或者使用其他方法转换
            Glide.with(itemView.getContext())
                    .load(science.getImage())
                    .into(imageView);

            // 设置点击事件
            itemView.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemClick(science);
                }
            });
        }
    }
}